package cn.com.thingo.hyintelligent.presenter;

import javax.inject.Inject;

import cn.com.thingo.hyintelligent.base.RxPresenter;
import cn.com.thingo.hyintelligent.base.contract.SelectGatewayContract;
import cn.com.thingo.hyintelligent.model.DataManager;
import cn.com.thingo.hyintelligent.model.bean.CommonBean;
import cn.com.thingo.hyintelligent.model.bean.GatewayListBean;
import cn.com.thingo.hyintelligent.util.LogUtil;
import cn.com.thingo.hyintelligent.util.RxUtil;
import cn.com.thingo.hyintelligent.util.ToastUtil;
import io.reactivex.functions.Consumer;

public class SelectGatewayPresenter extends RxPresenter<SelectGatewayContract.View> implements SelectGatewayContract.Presenter {


    private DataManager dataManager;

    @Inject
    public SelectGatewayPresenter(DataManager dataManager) {
        this.dataManager = dataManager;
    }


    @Override
    public void gatewayList(String token) {
        addSubscribe(dataManager.gatewayList(token)
                .compose(RxUtil.<GatewayListBean>rxSchedulerHelper())
                .subscribe(new Consumer<GatewayListBean>() {
                    @Override
                    public void accept(GatewayListBean gatewayListBean) throws Exception {
                        if (gatewayListBean.getStatus() == 1) {
                            mView.viewGatewayList(gatewayListBean);
                        } else {
                            ToastUtil.shortShow(gatewayListBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        LogUtil.d(throwable.getMessage());
                    }
                }));
    }

    @Override
    public void deleteGatewayById(String token, String gatewayId) {
        addSubscribe(dataManager.deleteGateway(token, gatewayId).compose(RxUtil.<CommonBean>rxSchedulerHelper())
                .subscribe(new Consumer<CommonBean>() {
                    @Override
                    public void accept(CommonBean commonBean) {
                        if (commonBean.getStatus() == 1) {
                            mView.viewDeleteGatewayById(commonBean);
                        } else {
                            ToastUtil.shortShow(commonBean.getMsg());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) {
                        LogUtil.d(throwable.getMessage());
                    }
                }));
    }
}
